package plantasvszombies;

import dominio.*;
import applicationModels.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteTextField;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.util.string.Strings;

@SuppressWarnings("serial")
public class AlmanaqueZombiesPage extends WebPage {

	
	private final WebPage mainPage;
	private final AlmanaqueZombies almanaque;
	private final Juego game;

	public AlmanaqueZombiesPage(WebPage mainPage,Juego g) {
		this.mainPage = mainPage;
		this.game = g;
		this.almanaque = new AlmanaqueZombies(game.getZombies());
		
		setDefaultModel(new CompoundPropertyModel<AlmanaqueZombies>(this.almanaque));
		
		addFormDeBusqueda();
		generarTablaResultados();
		addActions();
	}	 

	@SuppressWarnings("rawtypes")
	protected void addActions() {
		this.add(new Link("indexlink") {
		     public void onClick() {
		    	 this.setResponsePage(mainPage);
		     }
		});		
	}

	protected void addFormDeBusqueda() {
		Form<Zombie> resultadosBusquedaForm = new Form<Zombie>("form");
		this.generarCamposBusqueda(resultadosBusquedaForm);
		this.add(resultadosBusquedaForm);
	}

	protected void generarCamposBusqueda(Form<Zombie> parent){
		parent.add(new AutoCompleteTextField<String>("zombieABuscar"){
			@Override
            protected Iterator<String> getChoices(String input) {
                if (Strings.isEmpty(input)) {
                    List<String> emptyList = Collections.emptyList();
                    return emptyList.iterator();
                }

                List<String> choices = new ArrayList<String>(10);

                List<Zombie> zombies = game.getZombies();

                for (final Zombie aZombie : zombies) {
                    final String zombieName = aZombie.getNombre();

                    if (zombieName.toUpperCase().contains(input.toUpperCase())) {
                        choices.add(zombieName);
                        if (choices.size() == 10) {
                            break;
                        }
                    }
                }

                return choices.iterator();
            }
		}); 
	            
		parent.add(new Button("buscar") {
			@Override
			public void onSubmit() {
				almanaque.search();
			}
		});
	}
	
	private void generarTablaResultados() {
		this.add(new PropertyListView<Zombie>("resultado") {
			@Override
			protected void populateItem(final ListItem<Zombie> item) {
				item.add(new Label("nombre"));
				item.add(new Label("puntosAtaque"));
				item.add(new Label("puntosDefensa"));
			}
		});
	}
}
